package com.caifu.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.caifu.bean.Result;
import com.caifu.bean.ResultUtil;
import com.caifu.pojo.SysUser;
import com.caifu.service.SysUserService;
import com.caifu.util.CheckUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author tww
 * ClassName: SysUserController.java、
 * createTime: 2021年03月03日 14:45:42
 **/
@Controller
@RequestMapping("user")
public class SysUserController {

    private static final Logger logger = LoggerFactory.getLogger(SysUserController.class);

    @Resource
    private SysUserService sysUserService;


    private String prefix = "user";

    /**
     * 用户管理页
     */
    @GetMapping("toSysuserPage")
    public String toSysuserPage() {
        return prefix + "user";
    }

    /**
     * 添加
     */
    @PostMapping("save")
    @ResponseBody
    public Result save(SysUser sysUser) {
        try {
            return sysUserService.save(sysUser);
        } catch (Exception e) {
            String msg = "网络异常";
            if (CheckUtil.isNotNull(e.getMessage())) {
                msg = e.getMessage();
            }
            logger.error(e.toString());
            e.printStackTrace();
            return ResultUtil.error(500, msg);
        }
    }

    /**
     * 修改
     */
    @PostMapping("update")
    @ResponseBody
    public Result update(SysUser sysUser) {
        try {
            return sysUserService.update(sysUser);
        } catch (Exception e) {
            String msg = "网络异常";
            if (CheckUtil.isNotNull(e.getMessage())) {
                msg = e.getMessage();
            }
            logger.error(e.toString());
            e.printStackTrace();
            return ResultUtil.error(500, msg);
        }
    }


    /**
     * 获取用户列表
     *
     * @return
     */
    @GetMapping("getSysUserList")
    @ResponseBody
    public Result getSysUserList(SysUser sysUser, Integer currentPage, Integer pageSize) throws Exception {
        IPage<SysUser> sysUsers = sysUserService.getSysUserList(sysUser, currentPage, pageSize);
        return ResultUtil.getDataSuccess(sysUsers);
    }


    /**
     * 根据id删除
     *
     * @param id 用户id
     */
    @DeleteMapping("deleteById")
    @ResponseBody
    public Result deleteById(Integer id) {
        return sysUserService.deleteById(id);
    }

}
